package cn.pconline.officialclub.service;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.ClubStat;
import cn.pconline.autoclub.entity.OperateLog;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.MemberService;
import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.officialclub.entity.OActivity;
import cn.pconline.officialclub.entity.OClub;
import cn.pconline.officialclub.entity.OClubMember;
import cn.pconline.officialclub.entity.OCommonMember;
import cn.pconline.officialclub.entity.OMemberApplication;
import cn.pconline.officialclub.entity.OUserLock;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.Mid;
import org.gelivable.dao.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;

/**
 * 官方车友会会员相关
 *
 * @author <a href="mailto:chensy0203@163.com">chensy</a>
 */
public class OMemberService extends ServiceKit {

    @Autowired
    private OMessageService oMessageService;
    @Autowired
    private OActivityService oActivityService;
    @Autowired
    private ODynaService oDynaService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private ClubService clubService;
    @Autowired
    private OClubService oClubService;

    /**
     * 查普通成员
     *
     * @param userId
     * @param oClubId
     * @return
     */
    public OCommonMember findCommonMember(long userId, long oClubId) {
        return findById(OCommonMember.class, new Mid(userId, oClubId));
    }

    /**
     * 查车友会成员
     *
     * @param clubId
     * @param oClubId
     * @return
     */
    public OClubMember findClubMember(long clubId, long oClubId) {
        return findById(OClubMember.class, new Mid(clubId, oClubId));
    }

    /**
     * 查车友会入驻的申请
     *
     * @param memberApplicationId
     * @return
     */
    public OMemberApplication findMemberApplication(long memberApplicationId) {
        return findById(OMemberApplication.class, memberApplicationId);
    }

    /**
     * 统计车友会的会员
     *
     * @param oClubId
     * @return
     */
    public int countCommonMember(long oClubId) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select count(1) from ").appendSql(getTableName(OCommonMember.class));
        sql.appendSql(" where o_club_id = ").appendValue(oClubId);
        return count(sql);
    }

    /**
     * 统计车友会的会员
     *
     * @param oClubId
     * @return
     */
    public int countVipMember(long oClubId) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select count(1) from ").appendSql(getTableName(OCommonMember.class)).appendSql(" m,");
        sql.appendSql(getTableName(User.class)).appendSql(" u ");
        sql.appendSql(" where u.user_id = m.user_id and m.o_club_id = ").appendValue(oClubId).appendSql(" and u.vip = 1");
        return count(sql);
    }

    /**
     * 统计入驻车友会数量
     *
     * @param oClubId
     * @return
     */
    public int countClubMember(long oClubId) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select count(1) from ").appendSql(getTableName(OClubMember.class));
        sql.appendSql(" where o_club_id = ").appendValue(oClubId);
        return count(sql);
    }

    /**
     * 申请列表（民间车友会申请入驻车友会）
     *
     * @param clubId 车友会id
     * @param oClubId 官方车友会id
     * @param status 如果为NULL表示查全部
     * @return 申请
     */
    private List<OMemberApplication> listMemberApplication(Long clubId, Long oClubId, Integer status) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select id from ").appendSql(getTableName(OMemberApplication.class)).appendSql(" where 1=1 ");
        if (clubId != null) {
            sql.appendSql(" and club_id= ").appendValue(clubId);
        }
        if (oClubId != null) {
            sql.appendSql(" and o_club_id= ").appendValue(oClubId);
        }
        if (status != null) {
            sql.appendSql(" and status = ").appendValue(status);
        }
        sql.appendSql(" order by create_at desc");
        return list(OMemberApplication.class, sql);
    }

    /**
     * 官方车友会列出待审核的入会申请
     *
     * @param oClubId 车友会id
     * @return
     */
    public List<OMemberApplication> listMemberApplication4Audit(long oClubId) {
        return listMemberApplication(null, oClubId, OMemberApplication.STATUS_UNAUDITED);
    }

    /**
     * 官方车友会列出待审核的入会申请(包含车友会对象)
     *
     * @param oClubId 车友会id
     * @return
     */
    public List<OMemberApplication> listMemberApplication4AuditWithClub(long oClubId) {
        List<OMemberApplication> memberApplications = listMemberApplication(null, oClubId, OMemberApplication.STATUS_UNAUDITED);
        clubService.loadTransientClubs(memberApplications);
        return memberApplications;
    }

    /**
     * 民间车友会列出等审核的入会申请
     *
     * @param clubId
     * @return
     */
    public List<OMemberApplication> listMemberApplicationByClubId(long clubId) {
        return listMemberApplication(clubId, null, OMemberApplication.STATUS_UNAUDITED);
    }

    /**
     * 民间车友会申请加入官方车友会
     *
     * @return
     */
    public long createMemberApplication(long userId, long clubId, long oClubId) {
        OMemberApplication app = new OMemberApplication();
        Date now = DateUtils.getNow();
        app.setAuditAt(null);
        app.setAuditBy(0);
        app.setClubId(clubId);
        app.setCreateAt(now);
        app.setCreateBy(userId);
        app.setOClubId(oClubId);
        app.setStatus(OMemberApplication.STATUS_UNAUDITED);
        return geliDao.create(app);
    }

    /**
     * 民间车友会取消申请
     */
    public boolean deleteMemberApplication(long id) {
        OMemberApplication oa = findById(OMemberApplication.class, id);
        oa.setStatus(OMemberApplication.STATUS_CANCEL);
        return geliDao.update(oa, "status") > 0;
    }

    /**
     * 分页列出入驻的车友会
     *
     * @param oClubId 查某车友会时,车友会的id
     * @param pageNo 分页号
     * @param pageSize 每页显示
     * @return
     */
    public List<Club> listClubMember(long oClubId, int limit) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select club_id from ").appendSql(getTableName(OClubMember.class)).appendSql(" where 1=1 ");
        sql.appendSql(" and o_club_id= ").appendValue(oClubId);
        sql.appendSql(" order by join_at desc");
        return listLimit(Club.class, sql, limit);
    }

    /**
     * 分页列出入驻的车友会
     *
     * @param oClubId 查某车友会时,车友会的id
     * @param pageNo 分页号
     * @param pageSize 每页显示
     * @return
     */
    public Pager<Club> pageClubMember(long oClubId, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select club_id from ").appendSql(getTableName(OClubMember.class)).appendSql(" where 1=1 ");
        sql.appendSql(" and o_club_id= ").appendValue(oClubId);
        sql.appendSql(" order by join_at desc");
        return pagerService.pager(Club.class, sql, pageNo, pageSize);
    }

    /**
     * 分页列出官方车友会全部成员信息，管理员优先，然后按最新加入的会员优先
     *
     * @param oClubId 查某车友会时,车友会的id
     * @param pageNo 分页号
     * @param pageSize 每页显示
     * @return
     */
    public Pager<OCommonMember> pageCommonMember(long oClubId, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select user_id,o_club_id from ").appendSql(getTableName(OCommonMember.class)).appendSql(" where 1=1 ");
        sql.appendSql(" and o_club_id= ").appendValue(oClubId);
        sql.appendSql(" order by admin_type desc,join_at desc");
        Pager<OCommonMember> pager = pagerService.pager(OCommonMember.class, sql, pageNo, pageSize);
        loadUsers(pager);
        return pager;
    }

    /**
     * 分页列出官方车友会（普通|认证）成员信息，管理员优先，然后按最新加入的会员优先
     *
     * @param oClubId 查某车友会时,车友会的id
     * @param pageNo 分页号
     * @param pageSize 每页显示
     * @return
     */
    public Pager<OCommonMember> pageCommonMemberByVip(long oClubId, boolean isVip, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select m.user_id,o_club_id from ").appendSql(getTableName(OCommonMember.class)).appendSql(" m");
        sql.appendSql(" ,").appendSql(getTableName(User.class)).appendSql(" u ");
        sql.appendSql(" where m.user_id = u.user_id ");
        if (oClubId > 0) {
            sql.appendSql(" and o_club_id= ").appendValue(oClubId);
        }
        if (isVip) {
            sql.appendSql(" and u.vip=1");
        } else {
            sql.appendSql(" and u.vip=0");
        }
        sql.appendSql(" order by admin_type desc,join_at desc");
        Pager<OCommonMember> pager = pagerService.pager(OCommonMember.class, sql, pageNo, pageSize);
        loadUsers(pager);
        return pager;
    }

    /**
     * 分页列出官方车友会加入的车友会列表(新加入的车友会优先)
     *
     * @param userId
     * @return
     */
    public Pager<OClub> pageClub(long userId, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select o_club_id from ").appendSql(getTableName(OCommonMember.class));
        sql.appendSql(" where user_id=").appendValue(userId).appendSql(" order by join_at desc");
        return pagerService.pager(OClub.class, sql, pageNo, pageSize);
    }

    /**
     * 列出某车友会的管理员
     *
     * @param oClubId 车友会id
     * @return
     */
    public List<OCommonMember> listAdmin(long oClubId) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select user_id,o_club_id from ").appendSql(getTableName(OCommonMember.class));
        sql.appendSql(" where o_club_id = ").appendValue(oClubId);
        sql.appendSql(" and admin_type in ").appendValues(new Integer[]{OCommonMember.ADMIN_TYPE_PRESIDENT, OCommonMember.ADMIN_TYPE_VICE_PRESIDENT});
        sql.appendSql(" order by admin_type desc,join_at desc");
        return loadUsers(list(OCommonMember.class, sql));
    }

    /**
     * 关键字查询会员
     *
     * @param oClubId
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @param isMatchSeries 是否匹配车系
     * @return
     */
    public Pager<OCommonMember> pageCommonMemberBy(long oClubId, String keyword, int pageNo, int pageSize, boolean isMatchSeries) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select c.user_id,c.o_club_id from ").appendSql(getTableName(OCommonMember.class)).appendSql(" c,").appendSql(getTableName(User.class)).appendSql(" u where ");
        if (isMatchSeries) {
            sql.appendSql("(");
        }
        sql.appendSql(" u.nickname like").appendValue("%" + keyword + "%");
        if (isMatchSeries) {
            sql.appendSql(" or u.car_info like ").appendValue("%" + keyword + "%")
                    .appendSql(" or (u.vip=1 and u.vip_info like ").appendValue("%" + keyword + "%")
                    .appendSql("))");
        }
        sql.appendSql(" and c.user_id=u.user_id and c.o_club_id=").appendValue(oClubId);
        Pager<OCommonMember> members = pagerService.pager(OCommonMember.class, sql, pageNo, pageSize);
        loadUsers(members);
        return members;
    }

    /**
     * 普通会员加入官方车友会
     *
     * @param userId
     * @param oClubId
     */
    public void createCommonMember(long userId, long oClubId) {
        OCommonMember mem = new OCommonMember();
        mem.setAdminType(OCommonMember.ADMIN_TYPE_COMMON);
        mem.setJoinAt(DateUtils.getNow());
        mem.setOClubId(oClubId);
        mem.setUserId(userId);
        geliDao.create(mem);
    }

    /**
     * 根据条件批量入驻车友会，（根据条件，可能会有些数据量大一些）
     *
     * @param brandId
     * @param seriesId
     * @param provinceId
     * @param cityId
     * @param oClubId
     * @return
     */
    public int batchCreateClubMember(long brandId, long seriesId, long provinceId, long cityId, long oClubId) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select c.club_id clubId from ").appendSql(getTableName(Club.class));
        sql.appendSql(" c where 1=1");
        if (provinceId > 0) {
            sql.appendSql(" and c.province_id = ").appendValue(provinceId);
        }
        if (cityId > 0) {
            sql.appendSql(" and c.city_id = ").appendValue(cityId);
        }
        if (brandId > 0) {
            sql.appendSql(" and c.brand_id = ").appendValue(brandId);
        }
        if (seriesId > 0) {
            sql.appendSql(" and c.series_id = ").appendValue(seriesId);
        }
        sql.appendSql(" and c.club_id not in(select club_id from ").appendSql(getTableName(OClubMember.class)).appendSql(")");
        List<Map<String, Object>> list;
        try {
            list = jdbcTemplate.queryForList(sql.getSql(), sql.getValues());
        } catch (EmptyResultDataAccessException e) {
            list = Collections.emptyList();
        }
        for (Map<String, Object> m : list) {
            Long clubId = (Long) m.get("clubId");
            createClubMember(clubId, oClubId);
        }
        return list.size();
    }

    /**
     * 民间车友会入驻官方车友会 （批量插入官方车友会会员）
     *
     * @param clubId
     * @param oClubId
     */
    public void createClubMember(long clubId, long oClubId) {
        //车友会入驻
        OClubMember mem = new OClubMember();
        mem.setClubId(clubId);
        mem.setOClubId(oClubId);
        mem.setJoinAt(DateUtils.getNow());
        geliDao.create(mem);
        //车友会下的会员一起加入
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.appendSql("select user_id from ").appendSql(getTableName(ClubMember.class));
        sqlBuilder.appendSql(" where club_id = ").appendValue(clubId);
        List<Long> userIds = getIds(sqlBuilder);
        List<Object[]> vals = new ArrayList<Object[]>();
        String sql = "insert ignore into oac_common_member(user_id,o_club_id,join_at,admin_type) values(?,?,now(),?) ";
        for (Long id : userIds) {
            Object[] objs = new Object[]{id, oClubId, OCommonMember.ADMIN_TYPE_COMMON};
            vals.add(objs);
        }
        jdbcTemplate.batchUpdate(sql, vals);
        //入驻成功后更新会员数
        OClub oClub = oClubService.findValidClub(oClubId);
        if (oClub != null) {
            int memberNum = countCommonMember(oClubId);
            int clubNum = countClubMember(oClubId);
            int vipNum = countVipMember(oClubId);
            oClub.setVipNum(vipNum);
            oClub.setMemberNum(memberNum);
            oClub.setClubNum(clubNum);
            oClubService.updateClub(oClub);
        }
    }

    /**
     * 审核入会申请 会发系统通知
     *
     * @param application 申请
     * @param auditById 审核人id
     * @return
     */
    public boolean passMemberApplication(OMemberApplication application, long auditById) {
        application.setStatus(OMemberApplication.STATUS_PASS);
        application.setAuditBy(auditById);
        application.setAuditAt(DateUtils.getNow());
        int result = geliDao.update(application, "audit_at,audit_by,status");
        createClubMember(application.getClubId(), application.getoClubId());
        if (result > 0) {
            //通知会长
            Club club = findById(Club.class, application.getClubId());
            OClub oClub = findById(OClub.class, application.getoClubId());
            String clubLink = oMessageService.getClubLink(club.getClubUrl(), club.getName());
            String oClubLink = oMessageService.getOClubLink(oClub.getUrl(), oClub.getName());
            oMessageService.sendSysMsg(application.getCreateBy(), OMessageService.CLUB_APPLY_PASS, clubLink, oClubLink);
            return true;
        }
        return false;
    }

    /**
     * 审核入会申请 会发系统通知
     *
     * @param application 申请
     * @param auditById 审核人id
     * @return
     */
    public boolean rejectMemberApplication(OMemberApplication application, long auditById) {
        application.setStatus(OMemberApplication.STATUS_REJECT);
        application.setAuditBy(auditById);
        application.setAuditAt(DateUtils.getNow());
        int result = geliDao.update(application, "audit_at,audit_by,status");
        if (result > 0) {
            //通知会长
            Club club = findById(Club.class, application.getClubId());
            OClub oClub = findById(OClub.class, application.getoClubId());
            String clubLink = oMessageService.getClubLink(club.getClubUrl(), club.getName());
            String oClubLink = oMessageService.getOClubLink(oClub.getUrl(), oClub.getName());
            oMessageService.sendSysMsg(application.getCreateBy(), OMessageService.CLUB_APPLY_REFUSE, clubLink, oClubLink);
            return true;
        }
        return false;
    }

    /**
     * 转让会长
     *
     * @param userId
     * @param oClubId
     * @param operatorId
     */
    public int setPresident(OCommonMember user, OClub oClub, OCommonMember operator) {
        OperateLog oLog = new OperateLog();
        oLog.setUid(user.getUserId());
        oLog.setOperatorId(operator.getUserId());
        oLog.setCreateAt(DateUtils.getNow());
        oLog.setDetail("官方车友会-转让会长——设置新会长");
        geliDao.create(oLog);
        user.setAdminType(OCommonMember.ADMIN_TYPE_PRESIDENT);
        int count = geliDao.update(user, "admin_type");
        String oClubLink = oMessageService.getOClubLink(oClub.getUrl(), oClub.getName());
        oMessageService.sendSysMsg(user.getUserId(), OMessageService.USER_UP_PRESIDENT, oClubLink);
        return count;
    }

    /**
     * 取消会长（转让会长时调用）
     *
     * @param oClubId
     * @param operatorId
     */
    public int cancelPresident(OCommonMember user, OCommonMember operator) {
        if (user == null || operator == null) {
            return 0;
        }
        OperateLog oLog = new OperateLog();
        oLog.setUid(user.getUserId());
        oLog.setOperatorId(operator.getUserId());
        oLog.setCreateAt(DateUtils.getNow());
        //自己操作自己时为转让会长,否则为解除副会长
        if (user.getUserId() == operator.getUserId()) {
            oLog.setDetail("官方车友会-转让会长——自己取消会长");
        } else {
            oLog.setDetail("官方车友会-转让会长——解除副会长");
        }
        geliDao.create(oLog);
        user.setAdminType(OCommonMember.ADMIN_TYPE_COMMON);
        return geliDao.update(user, "admin_type");
    }

    /**
     * 更新车友会的vip人数冗余字段
     *
     * @param club
     */
    public void updateVipNum(OClub club) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select count(1) from ").appendSql(getTableName(OCommonMember.class)).appendSql(" i,").appendSql(getTableName(User.class)).appendSql(" u ");
        sql.appendSql(" where i.user_id = u.user_id and i.o_club_id = ").appendValue(club.getoClubId());
        sql.appendSql(" and u.vip = 1");
        int num = count(sql);
        if (club.getVipNum() != num) {
            club.setVipNum(num);
            geliDao.update(club, "vip_num");
        }
    }

    /**
     * 更新车友会的人数冗余字段
     *
     * @param club
     */
    public void updateMemberNum(OClub club) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select count(1) from ").appendSql(getTableName(OCommonMember.class));
        sql.appendSql(" where o_club_id = ").appendValue(club.getoClubId());
        int num = count(sql);
        if (club.getMemberNum() != num) {
            club.setMemberNum(num);
            geliDao.update(club, "member_num");
        }
    }

    /**
     * 设置副会长
     *
     * @param userId
     * @param oClubId
     * @param operatorId
     */
    public int setVicePresident(OCommonMember user, OClub oClub, OCommonMember operator) {
        OperateLog oLog = new OperateLog();
        oLog.setUid(user.getUserId());
        oLog.setOperatorId(operator.getUserId());
        oLog.setCreateAt(DateUtils.getNow());
        oLog.setDetail("官方车友会-设置副会长");
        geliDao.create(oLog);
        user.setAdminType(OCommonMember.ADMIN_TYPE_VICE_PRESIDENT);
        int count = geliDao.update(user, "admin_type");
        String oClubLink = oMessageService.getOClubLink(oClub.getUrl(), oClub.getName());
        oMessageService.sendSysMsg(user.getUserId(), OMessageService.USER_UP_VICE_PRESIDENT, oClubLink);
        return count;
    }

    /**
     * 普通会员移出车友会
     *
     * @param userId
     * @param oClubId
     * @param operatorId
     * @return
     */
    public boolean expelMember(OCommonMember member, OClub oClub, long operatorId) {
        if (member == null || oClub == null) {
            return false;
        }
        int result = geliDao.delete(member, member.getId());
        if (result > 0) {
            OperateLog oLog = new OperateLog();
            oLog.setUid(member.getUserId());
            oLog.setOperatorId(operatorId);
            oLog.setCreateAt(DateUtils.getNow());
            oLog.setDetail("官方车友会-开除会员");
            geliDao.create(oLog);
            User user = findById(User.class, member.getUserId());
            if (user.getVip() == 1) {
                //更新认证人数
                updateVipNum(oClub);
            }
            //更新会员会数
            updateMemberNum(oClub);
            //删除创建的活动
            oActivityService.removeActivityBy(member.getUserId(), oClub.getoClubId());
            //删除参与的活动报名
            oActivityService.removeActivityMemberBy(member.getUserId(), oClub.getoClubId());
            //更新参与的活动的报名人数
            List<OActivity> activitys = oActivityService.listActivity4Join(member.getUserId(), oClub.getoClubId());
            for (OActivity o : activitys) {
                oActivityService.updateActivityJoinNum(o.getActivityId());
            }
            //删除所有动态索引
            cn.pconline.autoclub.entity.ClubMember clubMember = memberService.findClubMemberByUserId(member.getUserId());
            long clubId = (clubMember == null?0:clubMember.getClubId());
            oDynaService.deleteClubDynaByUserId(oClub.getoClubId(), clubId, member.getUserId());
            return true;
        }
        return false;
    }

    /**
     * 查询申请记录
     *
     * @param clubId
     * @param status
     * @return
     */
    public OMemberApplication findMemberApplicationByClub(long clubId, int status) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select * from ").appendSql(getTableName(OMemberApplication.class)).appendSql(" where club_id = ");
        sql.appendValue(clubId).appendSql(" and status=").appendValue(status);
        return findFirst(OMemberApplication.class, sql);
    }

    /**
     * 创建会员对象
     *
     * @param oCommonMember
     * @return
     */
    public long createCommonMember(OCommonMember oCommonMember) {
        return geliDao.create(oCommonMember);
    }

    /**
     * 取消入驻
     *
     * @param oClubMember
     * @return
     */
    public int deleteClubMember(OClubMember oClubMember) {
        return geliDao.delete(oClubMember, new Mid(oClubMember.getClubId(), oClubMember.getoClubId()));
    }

    /**
     * 查询最新会员
     *
     * @param oClubId
     * @param limit
     * @return
     */
    public List<OCommonMember> listNewMembers(long oClubId, int limit) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select * from ").appendSql(getTableName(OCommonMember.class)).appendSql(" where o_club_id=").appendValue(oClubId).appendSql(" order by join_at desc");
        return listLimit(OCommonMember.class, sql, limit);
    }

    /**
     * 分页列出入驻的车友会(根据排名排序)
     *
     * @param oClubId 查某车友会时,车友会的id
     * @param pageNo 分页号
     * @param pageSize 每页显示
     * @return
     */
    public Pager<Club> pageClubMemberOrderByRank(long oClubId, int pageNo, int pageSize) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select b.club_id from ").appendSql(getTableName(Club.class)).appendSql(" b LEFT JOIN ").appendSql(getTableName(OClubMember.class)).appendSql(" c on c.club_id=b.club_id  LEFT JOIN ").appendSql(getTableName(ClubStat.class)).appendSql(" s ON c.club_id=s.club_id ").appendSql(" where ");
        sql.appendSql("  o_club_id= ").appendValue(oClubId);
        sql.appendSql(" order by s.rank_petrol desc, b.total_petrol desc, b.create_at");
        return pagerService.pager(Club.class, sql, pageNo, pageSize);
    }
    /**
     * 查询用户在某个品牌车友会是否被锁
     * @param user
     * @param clubId
     * @return 
     */
     public  boolean isUserLock(User user,long clubId){
        if(user == null || user.getUid() == 0 ) return false;
        String phone =  user.getBindPhone();
        if(StringUtils.isBlank(phone) || clubId == 0){
            return false;
        }
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql("select count(1) from ").appendSql(getTableName(OUserLock.class)).appendSql(" where o_club_id=").appendValue(clubId).appendSql(" and phone=").appendValue(phone);
        return count(sql)>0;
    }
     public boolean lockUser(User user,long clubId){
         if(user == null || user.getUid() == 0) return false;
          String phone =  user.getBindPhone();
        if(StringUtils.isBlank(phone) || clubId == 0){
            return false;
        }
        OUserLock userLock = new OUserLock();
        userLock.setoClubId(clubId);
        userLock.setPhone(phone);
        userLock.setCreateAt(DateUtils.getNow());
        long id = geliDao.create(userLock);
        return id>0;
     }
}
